package com.zzyl.serve.service.impl;


import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    @Transactional
    public NursingPlanVo selectNursingPlanById(Long id)
    {
//        //1、查询护理计划主表数据
//        NursingPlan nursingPlan = getById(id);
//        if(ObjectUtil.isEmpty(nursingPlan)){
//            return null;
//        }
//        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
//
//        //2、查询护理计划项目计划子表数据
//        List<NursingProjectPlan> List = nursingProjectPlanService.lambdaQuery()
//                .eq(NursingProjectPlan::getPlanId, id)
//                .list();
//        if(ObjectUtil.isNotEmpty(List)){
//        List<NursingProjectPlanVo> projectPlanVoList = List.stream().map(projectPlan -> {
//            NursingProjectPlanVo projectPlanVo = BeanUtil.toBean(projectPlan, NursingProjectPlanVo.class);
//            projectPlanVo.setProjectId(String.valueOf(projectPlan.getProjectId()));
//            projectPlanVo.setExecuteCycle(String.valueOf(projectPlan.getExecuteCycle()));
//            projectPlanVo.setExecuteFrequency(projectPlan.getExecuteFrequency().longValue());
//            return projectPlanVo;
//        }).collect(Collectors.toList());
//        nursingPlanVo.setProjectPlans(projectPlanVoList);
//    }
//        return nursingPlanVo;
        //方式2
        return nursingPlanMapper.selectNursingPlanVoById(id);
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //1、保存护理计划主表数据
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        save(nursingPlan);
        //2、批量保存护理计划项目 子表数据
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if(ObjectUtil.isEmpty(projectPlans)){
            return 1;
        }
        projectPlans.forEach(projectPlan -> {
            projectPlan.setPlanId(nursingPlan.getId());
        });
        boolean result = this.nursingProjectPlanService.saveBatch(projectPlans);

        //3、返回结果
        return result ? 1 : 0;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //1、修改护理计划主表数据
        //1.1 删除护理计划项目计划子表数据
//        LambdaQueryWrapper<NursingProjectPlan> eq = Wrappers
//                .<NursingProjectPlan>lambdaQuery()
//                .eq(NursingProjectPlan::getPlanId, nursingPlanDto
//                        .getId());
//        nursingProjectPlanService.remove(eq);
        this.nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery()
                .eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId()));

        //1.2 判断前端是否有提交护理计划项目计划子表数据，如果有则保存
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if(ObjectUtil.isNotEmpty(projectPlans)){
            projectPlans.forEach(projectPlan -> projectPlan.setPlanId(nursingPlanDto.getId()));
                this.nursingProjectPlanService.saveBatch(projectPlans);

        }
        //2、批量修改护理计划项目计划子表数据
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        //3、返回结果
        return this.updateById(nursingPlan) ? 1 : 0;

    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }
}
